package be.myandroid.games.menu;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import be.myandroid.games.StateManager;
import be.myandroid.games.TouchManager;
import be.myandroid.games.TouchManager.touchListener;
import be.myandroid.games.spritetext.LabelMaker;
import be.myandroid.games.spritetext.Projector;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.widget.Toast;


public class PauseMenu extends AbstractMenu implements touchListener{
    private ArrayList<GameMenuItem> items = new ArrayList<GameMenuItem>();
    private GameMenuItem resumeItem;
    private StateManager state = StateManager.getInstance();
    
	public PauseMenu(final Context context) {
        items.add(new GameMenuItem("New Game"){
        	@Override
        	public void OnClick() {
        		state.currentGame.reset();
        	}
        });
        items.add(new GameMenuItem("High Score"){
        	@Override
        	public void OnClick() {
        		Toast.makeText(context, "...", 1).show();
        	}
        });
        resumeItem = new GameMenuItem("Resume"){
        	@Override
        	public void OnClick() {
        		state.TogglePauze();
        	}
        };
        items.add(resumeItem);
        
        TouchManager.getInstance().addListener(this);
	}

	@Override
	public void initialize(GL10 gl, Drawable background, LabelMaker parent, Paint paint) {
        for (GameMenuItem item : items) {
        	item.initialize(gl, background, parent, paint);
        }
	}

	@Override
	protected void layoutItems(float mWidth, float mHeight) {
		
		resumeItem.visible = state.Paused();
		resumeItem.enable = state.Paused();
		
		float totalHeight = 0.0f;
		float sepp = 0.0f;
		
		if(!(mWidth != 0 & mHeight != 0))
			return;
		
		for (GameMenuItem item : items) {
			/*item.width = mLabels.getWidth(item.getId()) * 2.0f / mWidth;
			item.height = mLabels.getHeight(item.getId()) * 2.0f / mHeight;*/
			item.layout(mWidth, mHeight);
			if(!item.visible)
				continue;
			totalHeight += item.getHeight();
		}
		
		totalHeight += sepp * (items.size() - 1);
		
		float posH = totalHeight * 0.5f;
		for(GameMenuItem item : items) {
			if(!item.visible)
				continue;
			item.y = (posH - item.getHeight() * 0.5f);
			posH -= item.getHeight() + sepp;
		}
	}

	/*public void isChanged(State newState) {
		parent.notifyChange();
	}*/

	@Override
	public void draw(GL10 gl, LabelMaker mLabels, Projector mProjector) {
        for (GameMenuItem item : items) {
			item.draw(gl, mLabels, mProjector);
		}
	}
	
	public void OnClick(float xPos, float yPos) {
		if(state.Paused() | state.Menu())
		for (GameMenuItem item : items) {
			item.OnClick(xPos, yPos);
		}
	}
}
